{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MongoClient(host=['192.168.168.126:27017'], document_class=dict, tz_aware=False, connect=True)\n"
     ]
    }
   ],
   "source": [
    "from pymongo import MongoClient\n",
    "uri = \"mongodb://192.168.168.126:27017\"\n",
    "client = MongoClient(uri)\n",
    "print(client)\n",
    "\n",
    "db = client.exam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name1_1': 'test0', 'arr_1': [{'a': 0, 'b': 2}, {'a': 1, 'b': 2}]}\n",
      "{'name1_1': 'test1', 'arr_1': [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}]}\n",
      "{'name1_1': 'test2', 'arr_1': [{'a': 2, 'b': 2}, {'a': 3, 'b': 2}]}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-30-9fb8c532309d>:4: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.\n",
      "  db.nestedUpdate.insert(data)\n"
     ]
    }
   ],
   "source": [
    "for i in range(3):  \n",
    "    data =dict(name1_1= str(\"test{}\".format(i) ),arr_1=[{\"a\": i,\"b\": 2}, {\"a\":int(i + 1) ,\"b\": 2}])\n",
    "    print(data)\n",
    "    db.nestedUpdate.insert(data)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更新arr_1数组中，a = 1 的对象，更新为 {a:11,b:12} 运行更新代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.UpdateResult at 0x24b2198d640>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.nestedUpdate.update_many({\n",
    "    'arr_1.a':1\n",
    "},{\n",
    "    '$set':{\n",
    "        'arr_1.$.a':11,\n",
    "        'arr_1.$.b':12\n",
    "    }\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('61cc0d44baba6f732ccf9d64'), 'name1_1': 'test0', 'arr_1': [{'a': 0, 'b': 2}, {'a': 11, 'c': [1, 2, 3]}]}\n",
      "{'_id': ObjectId('61cc0d44baba6f732ccf9d65'), 'name1_1': 'test1', 'arr_1': [{'a': 11, 'c': [1, 2, 3]}, {'a': 2, 'b': 2}]}\n",
      "{'_id': ObjectId('61cc0d44baba6f732ccf9d66'), 'name1_1': 'test2', 'arr_1': [{'a': 2, 'b': 2}, {'a': 3, 'b': 2}]}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for obj in db.nestedUpdate.find():\n",
    "    print(obj)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更新 a= 11 的对象值（与上面不同，上面是更新对象里面的一个值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.UpdateResult at 0x24b2198b100>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.nestedUpdate.update_many({\n",
    "    'arr_1.a': 11\n",
    "}, {\n",
    "    \"$set\": {'arr_1.$': {'a':11, 'c':[1,2,3]} \n",
    "    } \n",
    "})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "继续编辑，修改 arr_1.c 的元素，很容易想到如下"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "73e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
